// pages/modifyPassword/modifyPassWord.js
// 修改密码页
Page({

  /**
   * 页面的初始数据
   */
  data: {
    delIconImg: "/assets/icons/del.png",
    passwordIconImg: "/assets/icons/password.png",
    delIconHidden1: true,
    delIconHidden2: true,
    delIconHidden3: true,
    oldPassWord: "",
    newPassWord: "",
    newPassWord2: "",
    phone: ""
  },

  // 监听表单输入,显示/隐藏删除icon
  changeInput: function(e) {
    if (e.detail.value.length > 0) {
      if (e.currentTarget.id == 1) {
        this.setData({
          delIconHidden1: false
        })
      } else if (e.currentTarget.id == 2) {
        this.setData({
          delIconHidden2: false
        })
      } else if (e.currentTarget.id == 3) {
        this.setData({
          delIconHidden3: false
        })
      }
    }
    // 数据双向绑定
    let dataset = e.currentTarget.dataset
    let value = e.detail.value
    this.data[dataset.obj] = value
    this.setData({
      obj: this.data[dataset.obj]
    })
  },
  // input失焦触发,显示/隐藏删除icon
  blurInput: function(e) {
    if (e.detail.value.length <= 0) {
      if (e.currentTarget.id == 1) {
        this.setData({
          delIconHidden1: true
        })
      } else if (e.currentTarget.id == 2) {
        this.setData({
          delIconHidden2: true
        })
      } else if (e.currentTarget.id == 3) {
        this.setData({
          delIconHidden3: true
        })
      }
    }
  },
  // 点击删除图标清空input内容
  delInputVal: function(e) {
    if (e.currentTarget.dataset.id == 1) {
      this.setData({
        oldPassWord: '',
        delIconHidden1: true,
      })
    } else if (e.currentTarget.dataset.id == 2) {
      this.setData({
        newPassWord: '',
        delIconHidden2: true,
      })
    } else if (e.currentTarget.dataset.id == 3) {
      this.setData({
        newPassWord2: '',
        delIconHidden3: true,
      })
    }
  },

  submitModifyPassWord: function() {
    // 点击确定提交修改密码
    let that = this
    let npw = that.data.newPassWord
    let npw2 = that.data.newPassWord2
    let config = wx.getStorageSync('config')
    try {
      let phone = wx.getStorageSync('phone')
      if (phone.length > 0) {
        that.setData({
          phone: phone
        })
      }
    } catch (e) {
      console.log(e)
    }
    wx.request({
      url: config.host + 'api/user/findPassword',
      method: 'POST',
      data: {
        passWord: that.data.oldPassWord,
        userName: that.data.phone
      },
      success(res) {
        if (res.data.status == 400) {
          wx.showToast({
            title: '原密码输入错误',
            icon: 'none'
          })
        } else if (res.data.msg == 'ok') {
          // 判断用户两次新密码是否一致
          if (npw === npw2) {
            // 提交
            wx.request({
              url: config.host + 'api/user/updatePassWord',
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                userName: that.data.phone,
                newPassWord: that.data.newPassWord,
                oldPassWord: that.data.oldPassWord
              },
              success(res) {
                if (res.data.status == 200) {
                  // 修改密码成功
                  wx.showToast({
                    title: res.data.msg,
                    success: function() {
                      setTimeout(() => {
                        wx.reLaunch({
                          url: '/pages/my/my',
                        })
                      }, 300)
                    }
                  })
                }
              }
            })
          } else if (npw !== npw2) {
            wx.showToast({
              title: '密码不一致，请重新输入',
              icon: 'none'
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  }
})